package com.ihd.ihardware.view.tzc.discovery.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.databinding.ActivityWebBinding;
import com.ihd.ihardware.pojo.EmptyRes;
import com.ihd.ihardware.view.tzc.discovery.viewmodel.WebViewModel;
import com.just.agentweb.AgentWeb;

import cn.wowjoy.commonlibrary.app.Constans;
import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.widget.MDialog;
import cn.wowjoy.commonlibrary.widget.titlebar.TitleBar;

public class WebActivity extends BaseActivity<ActivityWebBinding, WebViewModel> {

    private String title;
    private String id;
    private String count;
    private boolean collectState;
    private AgentWeb mAgentWeb;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.keyboardEnable(true).statusBarDarkFont(true).init();
        title = getIntent().getStringExtra("title");
        id = getIntent().getStringExtra("url");
        count = getIntent().getStringExtra("count");
        collectState = getIntent().getBooleanExtra("collectState", false);

        binding.mtitlebar.setTitle(title);
        binding.mtitlebar.setLeftBack(this);
        binding.mtitlebar.addAction(new TitleBar.Action() {
            @Override
            public String getText() {
                return null;
            }

            @Override
            public int getDrawable() {
                return R.drawable.btn_share;
            }

            @Override
            public void performAction(View view) {

            }
        });
        binding.commentTV.setText(count);
        if (collectState)
            binding.collectIV.setImageResource(R.drawable.btn_article_liked);
        else
            binding.collectIV.setImageResource(R.drawable.btn_article_like);

        binding.commentET.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND && binding.commentET.getText().length() > 0) {
                    viewModel.sendArtComment(id, binding.commentET.getText().toString());
                    binding.commentET.setText("");
                }
                return false;
            }
        });

        binding.commentIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(WebActivity.this, CommentActivity.class);
                i.putExtra("articleId", id);
                i.putExtra("title", title);
                startActivityForResult(i, 200);
            }
        });

        binding.collectIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (collectState)
                    viewModel.collectCancel(id);
                else
                    viewModel.artCollect(id);
            }
        });

        setRx(AppConstans.SENDARTCOMMENT, new BaseConsumer<EmptyRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(EmptyRes mLiveData) {
                DialogUtils.successDialog(WebActivity.this, MDialog.DG_TYPE.SUCCESS, R.drawable.toasticon_suc, "评论成功");
                binding.commentTV.setText(String.valueOf(Integer.parseInt(count) + 1));
            }

            @Override
            protected void onError(Throwable error) {
            }
        });

        setRx(AppConstans.ARTCOLLECT, new BaseConsumer<EmptyRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(EmptyRes mLiveData) {
                collectState = true;
                binding.collectIV.setImageResource(R.drawable.btn_article_liked);
            }

            @Override
            protected void onError(Throwable error) {
            }
        });

        setRx(AppConstans.ARTCOLLECTCANCEL, new BaseConsumer<EmptyRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(EmptyRes mLiveData) {
                collectState = false;
                binding.collectIV.setImageResource(R.drawable.btn_article_like);
            }

            @Override
            protected void onError(Throwable error) {
            }
        });

        String token = SPUtils.getString(Constans.GATEWAY_TOKEN, "");
        JsonObject mParam = new JsonParser().parse(token).getAsJsonObject();

        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(binding.FL, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .additionalHttpHeader("amazingToken", mParam.get("userToken").getAsString())
//                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
                .createAgentWeb()
                .ready()
                .go("http://api.ruichon.com/marketingcenter/api/v1/articleHtml/detail?id=" + id);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    protected Class getViewModelClass() {
        return WebViewModel.class;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mAgentWeb.handleKeyEvent(keyCode, event)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAgentWeb.getWebLifeCycle().onDestroy();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Bundle extras = data.getExtras();
        switch (requestCode) {
            case 200:
                count = extras.getString("count");
                binding.commentTV.setText(count);
                break;
        }
    }
}
